import hashlib
import json

import jwt
from django.core.paginator import Paginator
from django.forms.models import model_to_dict
from django.http import  JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_http_methods

from .models import User,bumentable,yuangtable,kaoqin
# 判断是否为空
def is_empty(c):
    return c is None or str(c).split()==""
# 解密
def jiemi(pwd):
    return hashlib.md5(pwd.encode()).hexdigest()

@csrf_exempt
def login(request):
    # 使用post方法
    if request.method=="POST":
        data = json.loads(request.body)
        # 定义username和password
        username = data["username"]
        password = data["password"]
        # 判断用户和密码是否为空
        if is_empty(username) or is_empty(password):
            return JsonResponse({"error": "账号密码为空"})
        # 判断用户名是否正确
        if User.objects.filter(username=username).exists() == False:
            return JsonResponse({"error": "用户名错误"})
        yong = User.objects.get(username=username)
        # 判断密码是否正确
        if yong.password != jiemi(password):
            return JsonResponse({"error": "密码错误"})
        # 加密
        mi = "159357"
        pay = {"yong_id": yong.id}
        # 传token值
        token = jwt.encode(pay, mi)
        return JsonResponse({"message": "登录成功", "token": token})
@csrf_exempt
def select(request):
    # 查询部门表
    if request.method=="GET":
        all_data=bumentable.objects.all()
        list=[]
        # 循环打印部门表
        for intem in all_data:
            list.append(model_to_dict(intem))
        # 查询成功
        return JsonResponse({"message":"查询成功","data":list})

@csrf_exempt
@require_http_methods(["DELETE"])
def delete(request,id):
    # 离职校验考勤判断如果存在就离职了
    if kaoqin.objects.filter(ygid_id=id).exists()==True:
        return JsonResponse({"error":"离职前校验考勤记录"})
    kaoqin.objects.get(id=id).delete()
    # 离职成功
    return JsonResponse({"message":"离职成功"})
@csrf_exempt
@require_http_methods(["GET"])
def select1(request):
    data_all=yuangtable.objects.all()
    l = len(data_all)
    # 条件查询
    id=request.GET.get("id")
    if id:
        data_all=yuangtable.objects.filter(subm_id=id)
    ygname = request.GET.get("ygname", "")
    if ygname:
        data_all = data_all.filter(ygname=ygname)
    # 分页查询
    page_size=request.GET.get("page_size") or 8
    page=request.GET.get("page",1)
    p=Paginator(data_all,page_size)
    data_all=p.get_page(page)
    list=[]
    for intem in data_all:
        list.append(model_to_dict(intem))

    # 员工表查询成功
    return JsonResponse({"message":"查询成功","data":list,"total":l})
@require_http_methods(["POST"])
def create(request):
    data=json.loads(request.body)
    jia=kaoqin.objects.create(
        ygid_id=data["ygid_id"],
        kqday=data["kqday"],
        kqtype=data["kqtype"],
        beizhu=data["beizhu"]

    )

    return JsonResponse({"message":"创建成功","data":model_to_dict(jia)})
@require_http_methods(["GET"])
def select2(request):
    all_data=kaoqin.objects.all()
    l=len(all_data)
    # 条件查询
    kqtype=request.GET.get("kqtype","")
    if kqtype:
        all_data=all_data.filter(kqtype=kqtype)
    # 分页
    page_size = request.GET.get("page_size") or 3
    page = request.GET.get("page", 1)
    p = Paginator(all_data, page_size)
    all_data = p.get_page(page)
    list=[]
    for inetm in all_data:
        list.append(model_to_dict(inetm))
    # 查询成功
    return JsonResponse({"message":"查询成功","data":list,"total":l})
@require_http_methods(["PUT"])
def unique(request,id):
    data=json.loads(request.body)
    xg=yuangtable.objects.get(id=id)
    xg.ygname=data["ygname"]
    xg.subm_id=data["subm_id"]
    xg.price=data["price"]
    xg.ruday=data["ruday"]
    xg.zaizhistatus=data["zaizhistatus"]
    xg.save()
    return JsonResponse({"message":"修改成功","data":model_to_dict(xg)})








